package com.example.web.exm;

import com.example.web.entity.Admin;

import com.example.web.service.LoginService;
import com.example.web.util.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.dto.ResultDTO;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/login")
public class LoginController{

    @Autowired
    private LoginService loginService;

    /**
     * 登陆页面
     * @return
     */
    @GetMapping("/page")
    public String loginPage(){

        return "login";
    }



    @PostMapping("/do")
    @ResponseBody
    public ResultDTO login(@Validated Admin admin, BindingResult bindingResult, HttpSession session) throws Exception {

        ValidatorUtil.showMsg(bindingResult);

        return  loginService.login(admin, session);
    }

    @PostMapping("/logout")
    public ResultDTO getLogout(HttpSession session, HttpServletResponse response) throws IOException {
        session.invalidate();
        ResultDTO mv = new ResultDTO();
        response.sendRedirect("/login/page");
        return mv;
    }

    }




